package com.naihe.anderson.sms.paintbrush;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.naihe.anderson.sms.util.FileUtil;

/*
 * 实现方式：
 * 采用了两层bitmap的方式：
 * 底层与表层bitmap：floorBitmap, surfaceBitmap;表层bitmap为透明色，否则会覆盖掉底层bitmap的图形
 * 当前画图东都在表层bitmap：surfaceBitmap。如果改变画笔，则将当前surfaceBitmap的内容绘制到底层bitmap：floorBitmap
 * 如果选择橡皮，则需要在底层bitmap上进行绘制，
 * 查看原图片，也是讲图片绘制到底层bitmap
 */
public class PaintBrushView extends View {

	private DrawBS drawBS = null;
	private Point evevtPoint;
	private Bitmap floorBitmap, surfaceBitmap , picBitmap;// 底层与表层bitmap
	private Canvas floorCanvas, surfaceCanvas;// bitmap对应的canvas

	private boolean isEraser = false;
	private boolean canDoPic = false;
	
	Bitmap newbm;

	public PaintBrushView(Context context){
		super(context);
	}
	
	public PaintBrushView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public PaintBrushView(Context context ,int viewWith, int viewHeigth) {
		super(context);

		// 初始化drawBS，即drawBS默认为DrawPath类
		drawBS = new DrawPath();
		evevtPoint = new Point();

//		picBitmap = drawableToBitmap(context.getResources().getDrawable(R.drawable.pic_shape));
		
		// 底层bitmap与canvas，
		floorBitmap = Bitmap.createBitmap(viewWith, viewHeigth, Bitmap.Config.ARGB_8888);
		floorCanvas = new Canvas(floorBitmap);
		
		// 表面bitmap。置于底层bitmap之上，用于赋值绘制当前的所画的图形；需要设置为透明，否则覆盖底部bitmap
		surfaceBitmap = Bitmap.createBitmap(viewWith, viewHeigth, Bitmap.Config.ARGB_8888);
		surfaceCanvas = new Canvas(surfaceBitmap);
		surfaceCanvas.drawColor(Color.TRANSPARENT);
	}

	public void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);

		// 将底层bitmap的图形绘制到主画布
		if(picBitmap == null){
			canvas.drawBitmap(floorBitmap, 0, 0, null);
		} else {
			canvas.drawBitmap(picBitmap, 0, 0, null);
			canvas.drawBitmap(floorBitmap, 0, 0, null);
		}

		// 判断选择的图形是否为橡皮
		if (isEraser) {
			// 如果是橡皮，让画笔在底层bitmap上进行操作，
			/*
			 * 传递底层Canvas参数。 调用相应的画图工具类方法,在底层bitmap上使用floorCanvas进行绘图
			 */
			drawBS.onDraw(floorCanvas);
			canvas.drawBitmap(floorBitmap, 0, 0, null);

		} else {
			// 如果不是橡皮，则让画笔在表层bitmap上进行操作，
			/*
			 * 传递表层Canvas参数。 调用相应画图工具类方法,在表层bitmap上使用surfaceCanvas进行绘图
			 */
			drawBS.onDraw(surfaceCanvas);
			canvas.drawBitmap(surfaceBitmap, 0, 0, null);
		}

	}

	// 触摸事件。调用相应的画图工具类进行操作
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if(!canDoPic) return true;
		
		evevtPoint.set((int) event.getX(), (int) event.getY());

		switch (event.getAction()) {

		case MotionEvent.ACTION_DOWN:
			drawBS.onTouchDown(evevtPoint);
			break;

		case MotionEvent.ACTION_MOVE:
			drawBS.onTouchMove(evevtPoint);
			/*
			 * 拖动过程中不停的将bitmap的颜色设置为透明（清空表层bitmap）
			 * 否则整个拖动过程的轨迹都会画出来
			 */
			surfaceBitmap.eraseColor(Color.TRANSPARENT);
			invalidate();
			break;

		case MotionEvent.ACTION_UP:
			drawBS.onTouchUp(evevtPoint);
			break;
		default:
			break;
		}
		return true;
	}

	// 选择图形，实例化相应的类
	public void setDrawTool(int i) {
		switch (i) {
		case 0:
			drawBS = new DrawLine();
			break;
		case 1:
			drawBS = new DrawRectangle();
			break;
		case 2:
			drawBS = new DrawCircle();
			break;
		case 3:
			drawBS = new DrawTriangle();
			break;
		case 4:
			drawBS = new DrawCube();
			break;
		case 5:
			drawBS = new DrawColumn();
			break;
		case 10:
			// 如果需要橡皮。则实例化重新设置画笔的构造方法
			drawBS = new DrawPath(10);// 橡皮
			break;
		default:
			drawBS = new DrawPath();
			break;
		}

		// 如果选择橡皮，isEraser = true
		if (i == 10) {
			isEraser = true;
		} else {
			isEraser = false;
		}
		// 如果重新选择了图形，则需要将表层bitmap上的图像绘制到底层bitmap上进行保存
		floorCanvas.drawBitmap(surfaceBitmap, 0, 0, null);
	}

	public void setCanDoPic(boolean canDoPic){
		this.canDoPic = canDoPic;
	}
	
	// 将图片存入内存卡
	public void savePicture(String draw_name) {
		String strPath = FileUtil.getPicNotePath();
		FileOutputStream fos = null;
		try {
			File fPath = new File(strPath);
			if (!fPath.exists()) {
				fPath.mkdir();
			}

			File f = new File(strPath + draw_name.trim());
			f.createNewFile();

			fos = new FileOutputStream(f);
			Bitmap b = null;
			destroyDrawingCache();
			setDrawingCacheEnabled(true);

			buildDrawingCache();
			b = getDrawingCache();

			if (b != null) {
				b.compress(Bitmap.CompressFormat.PNG, 100, fos);
				if (!b.isRecycled())
					b.recycle();
				b = null;
				System.gc();
			}

		} catch (Exception e) {
		} finally {
			if (fos != null) {
				try {
					fos.flush();
					fos.close();
				} catch (IOException e) {
				}
			}
		}
	}

	// 取出图片
	public void editPicture(String draw_name) {
		// 新建一个临时的bitmap用于放置读取到的图片
//		Bitmap tempBitmap = Bitmap.createBitmap(floorBitmap.getWidth(),
//				floorBitmap.getHeight(), Bitmap.Config.ARGB_8888);
//		if (!tempBitmap.isRecycled()) {
//			tempBitmap.recycle();
//			tempBitmap = null;
//			System.gc();
//		}
//
//		tempBitmap = getLoacalBitmap(draw_name);
		// 将tempBitmap中的图形绘制到floorBitmap
//		floorCanvas.drawBitmap(tempBitmap, 0, 0, null);
		picBitmap = getLoacalBitmap(draw_name);;
		invalidate();
	}

	private Bitmap getLoacalBitmap(String draw_name) {
		try {
			BitmapFactory.Options opt = new BitmapFactory.Options();
			opt.inSampleSize = 1;
			opt.inPreferredConfig = Bitmap.Config.RGB_565;
			opt.inPurgeable = true;
			opt.inInputShareable = true; // 获取资源图片
			FileInputStream fis = new FileInputStream(FileUtil.getPicNotePath() + draw_name);
			return BitmapFactory.decodeStream(fis, null, opt);
		} catch (FileNotFoundException e) {
			return null;
		}
	}

	public Bitmap drawableToBitmap(Drawable drawable) {
		int width = drawable.getIntrinsicWidth();
		int height = drawable.getIntrinsicHeight();
		Bitmap bitmap = Bitmap.createBitmap(width, height, drawable
				.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
				: Bitmap.Config.RGB_565);
		Canvas canvas = new Canvas(bitmap);
		drawable.setBounds(0, 0, width, height);
		drawable.draw(canvas);
		return bitmap;
	}
}
